<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.lang.Exception" %>
<%@ page import="com.ykzhujiang.lovespirit.util.PMF" %>
<%@ page import="com.ykzhujiang.lovespirit.util.Utility" %>
<%@ page import="com.ykzhujiang.lovespirit.util.DataManager" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="javax.cache.Cache" %>
<%@ page import="javax.cache.CacheException" %>
<%@ page import="javax.cache.CacheFactory" %>
<%@ page import="javax.cache.CacheManager" %>
<%@ page import="com.google.appengine.api.memcache.stdimpl.GCacheFactory" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.Notice" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.LSUser" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.Comment" %>
<%@ page import="com.ykzhujiang.lovespirit.analyzer.Analyzer" %>
<%@ page import="com.ykzhujiang.lovespirit.analyzer.FemaleAnalyzer" %>
<%@ page import="com.ykzhujiang.lovespirit.analyzer.MaleAnalyzer" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.LThread" %>
<%@ page import="java.util.logging.Logger" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<link href="css/main.css" type="text/css" rel="stylesheet"></link>
<script src="http://www.google.com/jsapi"></script>
<script>
    google.setOnLoadCallback(function(){$(document).ready(pageLoad);});
    google.load("jquery","1.3.2");
    google.load("jqueryui","1.7");
    function pageLoad()
    {
    }
</script>

<%
		Logger logger = Logger.getLogger("thread.jsp");
		int MAX_CONTENT_LENGTH = 500;
		int MAX_PIC_NUM = 4;
		int MAX_COMMENT_NUM = 5;
		int MIN_HOT = 5;
		HashMap props = new HashMap();
        props.put(GCacheFactory.EXPIRATION_DELTA, 10);
		String key = request.getParameter("k");
		Notice notice = (Notice)DataManager.getData(Notice.class, key, true, props);
		List<Comment> comments = null;
		MaleAnalyzer ma=new MaleAnalyzer();
		FemaleAnalyzer fa=new FemaleAnalyzer();
		LThread lt=new LThread();
		logger.info("got key:" + key);
		if(notice != null) {
    		//logger.info("notice:"+notice.getTitle()+","+notice.getContent());
    		lt.notices.clear();
			lt.notices.add(notice);
			String query2 = "select from " + Comment.class.getName() + " where sourceUrl == '" + key + 
    			"' order by date desc range 0, " + (MAX_COMMENT_NUM + 1);
    		comments = (List<Comment>)DataManager.getData(query2, false, null);
%>
	<li class="<%=ma.Analyze(lt)?"item_m":fa.Analyze(lt)?"item_f":"item"%>">
		<a class="<%=notice.getType()==Notice.TYPE_NORMAL?"title":"title-deleted"%>" href="<%=notice.getSourceUrl()%>" target="_blank">
			<%=notice.getTitle()%>
		</a>
		<sup class="hot">
			<%=notice.getHot()%>
		</sup>
<%
		if(comments != null && comments.size() != 0) {
%>		
		<sup class="comment-num">
			<%=comments.size()>MAX_COMMENT_NUM?"hot":comments.size()%>
		</sup>
<%
		}
%>
		<img id="img_<%=key.hashCode()%>" src="images/plus.png" onclick="$('#content_<%=key.hashCode()%>').slideToggle(300,
			function(){
				if($(this).is(':hidden')) {
					$('#img_<%=key.hashCode()%>').attr('src','images/plus.png');
				} else {
					$('#img_<%=key.hashCode()%>').attr('src','images/minus.png');
				}
			});">

		<span>
			<a class="writer"><%=notice.getWriter()%></a>
			<a class="site"><%=notice.getSite()%></a>
		</span>
		
		<div class="content" id="content_<%=key.hashCode()%>">
			<div class="content_main">
				<%=notice.getContent().substring(0,Math.min(notice.getContent().length(),MAX_CONTENT_LENGTH))+" ..."%>
				<div>
<%
	for(int k = 0, j = 0; notice.getResourceUrl() != null && j < MAX_PIC_NUM && k < notice.getResourceUrl().size(); k++) {
		String url = notice.getResourceUrl().get(k);
		if(Utility.IsPicture(url)) {
			j++;
%>
				<a href="<%=url%>" target="_blank">
					<img src="<%=url%>" border="0" height="100" onerror="this.src='/images/au.gif'">
				</a>
<%			
		}
	}
%>
				</div>		
			</div>
			<div id="comments-<%=key.hashCode()%>" class="comments">
			</div>
			<script>
				$('#comments-<%=key.hashCode()%>').load('comment.jsp?k=<%=Utility.EncodeUrl(key)%>&s=0&r='+Math.random());
			</script>
<%
	if(UserServiceFactory.getUserService().getCurrentUser() != null) {
%>			
			<div class="comment">
     			<div>
     			 <textarea id="comment-content-<%=key.hashCode()%>" name="content" rows="3" cols="60"></textarea>
     			</div>
      			<div>
      				<input id="comment-button-<%=key.hashCode()%>" type="button" value="评论" onclick="
      					$('#comment-button-<%=key.hashCode()%>').attr('disabled','disabled');
      					$.post('comment', {
    						src: '<%=key%>',
    						content: $('#comment-content-<%=key.hashCode()%>').val()
						}, function() { 
    						$('#comments-<%=key.hashCode()%>').load('comment.jsp?k=<%=Utility.EncodeUrl(key)%>&s=0&r='+Math.random());
							$('#comment-button-<%=key.hashCode()%>').removeAttr('disabled');
						});"/>
      			</div>
			</div>
<%
	}
%>	
		</div>
	</li>
<%
		}
%>